package com.pets.springboot_petshop.controller;

import com.pets.springboot_petshop.entity.Product;
import com.pets.springboot_petshop.entity.User;
import com.pets.springboot_petshop.entity.UserDTO;
import com.pets.springboot_petshop.entity.Userpet;
import com.pets.springboot_petshop.pojo.Result;
import com.pets.springboot_petshop.service.UserService;
import com.pets.springboot_petshop.service.UserpetService;
import com.pets.springboot_petshop.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/12 下午4:49 周六
 * @Description: 用户宠物文档控制层
 */
@RestController
@RequestMapping("/userpet")
public class UserpetController {
    @Autowired
    private UserpetService userpetService;
    @Autowired
    private UserService userService;

    //id查询
    @GetMapping("/selectUserpetid")
    private Result selectUserpetid(@RequestParam Integer id) {
        Userpet userpet = userpetService.selectUserpetid(id);
        if (userpet == null) {
            return Result.error("获取失败");
        }else {
            return Result.success(userpet);
        }
    }
    //用户id查询
    @GetMapping("/selectUserpet")
    private Result<List<Userpet>> selectUserpetUserid(@RequestHeader(name = "Authorization") String token) {
        // 根据令牌查询相应的用户信息
        Map<String, Object> map = JwtUtil.parseToken(token);
        int user_id = (int) map.get("id");
        System.out.println(" id = " + user_id);
        // 检查用户是否存在
        User user1 = userService.select(user_id);
        if (user1 == null) {
            return Result.error("用户不存在");
        } else {
            List<Userpet> userpet = userpetService.selectUserpetUserid(user_id);
            if (userpet == null) {
                return Result.error("获取失败");
            } else {
                return Result.success(userpet);
            }
        }
    }
    //查询所有
    @GetMapping("/findAll")
    private Result findAll() {
        List<Userpet> userpet = userpetService.findAll();
        if (userpet == null) {
            // 如果userpet为null，返回获取失败的消息
            return Result.error("获取失败");
        } else if (userpet.isEmpty()) {
            // 如果userpet不为空但为空列表，返回用户未建立宠物文档的消息
            return Result.error("用户未建立宠物文档");
        } else {
            // 如果userpet不为空且不为空列表，返回成功的结果
            return Result.success(userpet);
        }
    }
    //添加
    @PostMapping("/addUserpet")
    private Result addUserpet(@RequestBody Userpet userpet,@RequestHeader(name = "Authorization") String token) {
        // 根据令牌查询相应的用户信息
        Map<String, Object> map = JwtUtil.parseToken(token);
        int user_id = (int) map.get("id");
        System.out.println(" id = " + user_id);
        // 检查用户是否存在
        User user1 = userService.select(user_id);
        if (user1 == null) {
            return Result.error("用户不存在");
        } else {
            userpet.setUser_id(user_id);
            if (userpetService.addUserpet(userpet) > 0) {
                return Result.success("新增成功");
            } else {
                return Result.error("新增失败");
            }
        }
    }
    //删除
    @PostMapping("/deleteUserpetById")
    private Result deleteUserpetById(@RequestParam Integer id) {
        if( userpetService.deleteUserpetById(id) > 0){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }
    //修改数据
    @PostMapping("/updateUserpetById")
    private Result updateUserpetById(@RequestBody Userpet userpet) {
        if( userpetService.updateUserpetById(userpet) > 0){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }
    @PostMapping("/updateimg")
    public Result<Userpet> updateimg(@RequestBody Userpet userpet) {
        if (userpetService.updateimg(userpet) > 0) {
            return Result.success(userpetService.selectUserpetid(userpet.getId()));
        } else {
            return Result.error("修改数据失败");
        }
    }
}

